package com.sgb.kjwl.viewmodel.self;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class AddressModel extends HBaseViewModel {
    public ObservableField<List<UserAddressEntity>> UserAddressEntityList = new ObservableField();
    public MutableLiveData<Boolean> isRefreshEnd;

    private Context context;
    public MutableLiveData<Boolean> getIsRefreshEnd(Context context) {
        this.context = context;
        if (isRefreshEnd == null) {
            isRefreshEnd = new MutableLiveData<>();
        }
        return isRefreshEnd;

    }

    public XRecyclerView.LoadingListener loadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            getAddressModel();
        }

        @Override
        public void onLoadMore() {

        }
    };

    public AddressModel(@NonNull Application application) {
        super(application);
    }

    /*public AddressModel(AddressActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }*/

    @Override
    public void onResume() {
        super.onResume();
        getAddressModel();
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        /*APIManager.getInstance().getUserAPI().getUserAddressList().enqueue(new Callback<BaseEntity<ListResponse<List<UserAddressEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<UserAddressEntity>>>> call, Response<BaseEntity<ListResponse<List<UserAddressEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.showAddress(response.body().getData().getList());
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<UserAddressEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });*/

        isRefreshEnd.postValue(false);
        APIManager.getInstance().getUserAPI().getUserAddressList().enqueue(new ICallBack<BaseEntity<ListResponse<List<UserAddressEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<UserAddressEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                isRefreshEnd.postValue(true);
                UserAddressEntityList.set(data.getData().getList());
            }

            @Override
            public void onFail() {
                super.onFail();
                isRefreshEnd.postValue(true);
                UserAddressEntityList.set(new ArrayList<>());
            }
        });
    }

    public void delIdAddress(String id) {
        /*DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().delAddress(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity, response.body().getData().toString());
                    getAddressModel();

                }
                assert response.body() != null;
                MToast.showToast(activity, response.body().getMsg());

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });*/

        APIManager.getInstance().getUserAPI().delAddress(id).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                showShortSnack(data.getData().toString());
                getAddressModel();
            }
        });
    }

    public void overrideAddress(UserAddressEntity userAddressEntity) {
        /*DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().editAddress(userAddressEntity).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    MToast.showToast(activity, response.body().getMsg());
                    if (response.body().getState().equals("ok")) {
                        getAddressModel();
                    } else {
                        assert response.body() != null;
                        MToast.showToast(activity, response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });*/

        APIManager.getInstance().getUserAPI().editAddress(userAddressEntity).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                getAddressModel();
                showShortSnack(data.getMsg());
            }
        });
    }
}
